package com.qf.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class BaseServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request , HttpServletResponse response) throws ServletException, IOException {
        super.service ( request , response );

        request.setCharacterEncoding ( "utf-8" );
        response.setContentType ( "text/html;charest=utf-8" );

        try {
            Class   clazz = this.getClass ( );
            Object o = clazz.newInstance ( );
            //获取地址栏约定的格式
            //http://localhost:8080/Maven_Project_02_war/子类的模块的地址?methodName=子类的方法名
            String methodName = request.getParameter ( "methodName" );

            if (methodName==null){
                methodName="inedex";
            }
            Method method = clazz.getMethod ( methodName , HttpServletRequest.class , HttpServletResponse.class );

            method.invoke ( o,request,response );
        } catch (InstantiationException e) {
            e.printStackTrace ( );
        } catch (IllegalAccessException e) {
            e.printStackTrace ( );
        } catch (NoSuchMethodException e) {
            e.printStackTrace ( );
        } catch (InvocationTargetException e) {
            e.printStackTrace ( );
            throw new RuntimeException("父类解析方法出现问题了...") ;
        }


    }
    
}
